package cn.pegasus.teamwork.pegasus.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.pegasus.entity.BussUcenterArea;
import cn.pegasus.teamwork.pegasus.mapper.BussUcenterAreaMapper;
import cn.pegasus.teamwork.pegasus.service.BussUcenterAreaService;


/**
 * 区域管理
 * @author coco
 *
 */
@Controller
@RequestMapping("/area")
public class AreaController extends BaseManagerController<BussUcenterArea, BussUcenterAreaMapper> {
	
	 private static final Logger logger = LoggerFactory.getLogger(AreaController.class);
	 
	 @Autowired
	 private BussUcenterAreaService areaService;
	 
	 /**
	  * 查询区域信息
	  */
	 @RequestMapping(value = "/list",method = {RequestMethod.GET})
	 public ModelAndView list(@RequestParam Map<String, Object> map){
		 ModelAndView view = new ModelAndView("area/area-list");
		 if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		 try{
			 PageObject<BussUcenterArea> dicList = areaService.PageList(map);
			 view.addObject("list", dicList);
			 view.addObject("map", map);
		 }catch(Exception e){
			 logger.error("查询区域列表信息异常",e.getMessage());
		 }
		 return view;
	 }
	
	 /**
	  * 查询某个区域的列表
	  */
	 @ResponseBody
	 @RequestMapping(value = "/findList")
	 public Map<String,Object> findList(
			 @RequestParam(value="fid",required = false) String fid,
			 @RequestParam(value="areaLevel",required = false) Integer areaLevel){
		 Map<String,Object> result = new HashMap<String,Object>();
		 try{
			 List<BussUcenterArea> list = areaService.findList(fid, areaLevel);
			 result = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, list);
		 }catch(Exception e){
			 logger.error("查询某个区域异常,fid="+fid, e.getMessage());
			 result = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
		 }
		 return result;
	 }
	 /**
	  * 跳转新增页面
	  * type  1  编辑
	  */
	 @RequestMapping(value="/goAddArea" ,method = {RequestMethod.GET})
	 public ModelAndView goAddArea(@RequestParam(value="type",required = false) String type,
			 @RequestParam(value="id",required = false) Long id){
		 if(null == type){
			 ModelAndView view = new ModelAndView("area/area-add");
			 return view;
		 }else{
			 ModelAndView view = new ModelAndView("area/area-edit");
			 BussUcenterArea area = areaService.findAreaInfo(id);
			 view.addObject("areaInfo", area);
			 return view;
		 }
	 }
	 /**
	  * 查看区域详情
	  */
	 @RequestMapping(value="/findAreaInfo")
	 public ModelAndView findAreaInfo(@RequestParam(value="id",required = false) Long id){
		 ModelAndView view = new ModelAndView("area/area-info");
		 BussUcenterArea area = areaService.findAreaInfo(id);
		 view.addObject("areaInfo", area);
		 return view;
	 }
	 
	 /**
	  * 新增
	  */
	 @ResponseBody
	 @RequestMapping(value = "/addArea")
	 public Map<String, Object> addArea(@RequestParam Map<String, Object> map){
		Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
        	areaService.addArea(getAdmin().getId(), map);
            resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        } catch (Exception e) {
            logger.error("新增区域异常", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
        }
        return resultMap;
	 }
	 
	 /**
	  * 修改区域信息
	  * @param map
	  * @return
	  * id 
	  * areaName
	  * longitude
	  * latitude
	  */
	 @ResponseBody
	 @RequestMapping(value="/updateArea")
	 public Map<String,Object> updateArea(@RequestParam Map<String, Object> map){
		Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
        	areaService.update(getAdmin().getId(), map);
            resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        } catch (Exception e) {
            logger.error("修改区域异常", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
        }
        return resultMap;
	 }
	 
	 /**
	  * 删除
	  */
	 @ResponseBody
	 @RequestMapping(value="/delete")
	 public Map<String,Object> delete(@RequestParam(value="id",required = false) Long id){
		Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
        	int i = areaService.delete(id);
        	if(i == 1){
        		resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        	}else if(i==2){
        		resultMap = ReMessage.resultBack(ParameterEunm.ERROR_405_CODE, null);
        	}
        } catch (Exception e) {
            logger.error("删除区域异常", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
        }
        return resultMap;
	 }
}
